package com.zhn.evsi.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhn.evsi.common.RedisCache;
import com.zhn.evsi.domain.dto.evsi.counter.EvsiCounterDTO;
import com.zhn.evsi.domain.entity.evsi.EvsiCounter;
import com.zhn.evsi.exception.CustomException;
import com.zhn.evsi.mapper.EvsiCounterMapper;
import com.zhn.evsi.service.IEvsiCounterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class EvsiCounterServiceImpl extends ServiceImpl<EvsiCounterMapper, EvsiCounter> implements IEvsiCounterService {

    @Autowired
    private RedisCache redisCache;

    /**
     * 新增计数器
     */
    @Override
    public void addCounter(String counterName) {
        if (StrUtil.isEmpty(counterName)) {
            throw new CustomException("异常名称不存在，请修改后添加");
        }
        // TODO 获取邮箱
        String username = "1085155177@qq.com";
        List<EvsiCounterDTO> counterList = redisCache.getCacheObject("Counter:" + username);
        if (ObjectUtil.isNotNull(counterList)) {
            // 判断名称是否冲突
            counterList.forEach(v -> {
                if (ObjectUtil.equal(v.getName(), counterName)) {
                    throw new CustomException("异常名称重复，请修改后添加");
                }
            });
        }else {
            counterList = new ArrayList<>();
        }

        // 自动生成id
        counterList.add(EvsiCounterDTO.builder()
                .id(IdUtil.simpleUUID())
                .name(counterName)
                .day(0)
                .week(0)
                .weekOld(0)
                .month(0)
                .monthOld(0)
                .year(0)
                .yearOld(0)
                .plan(1)
                .mail(username)
                .build());

        redisCache.setCacheObject("Counter:" + username, counterList);
    }

    /**
     * 计数器修改
     * */
    @Override
    public void editCounter(EvsiCounterDTO counterDTO) {
        String username = "1085155177@qq.com";
        List<EvsiCounterDTO> counterList = redisCache.getCacheObject("Counter:" + username);
        counterList.forEach(v -> {
            if (ObjectUtil.equal(v.getId(), counterDTO.getId())){
                v.setName(counterDTO.getName());
            }
        });
        redisCache.setCacheObject("Counter:" + username, counterList);
    }

    /**
     * 查询计数器
     * */
    @Override
    public List<EvsiCounterDTO> getCounter() {
        // TODO 查询用户名
        String username = "1085155177@qq.com";
        List<EvsiCounterDTO> counterList = redisCache.getCacheObject("Counter:" + username);
        return counterList;
    }

    /**
     * 计数器步进
     * */
    @Override
    public void stepCounter(EvsiCounterDTO counterDTO) {

        if (StrUtil.isEmpty(counterDTO.getId())) {
            throw new CustomException("参数错误，id不存在");
        }
        // TODO 查询用户名
        String username = "1085155177@qq.com";
        List<EvsiCounterDTO> counterList = redisCache.getCacheObject("Counter:" + username);
        counterList.forEach(v -> {
            if (ObjectUtil.equal(v.getId(), counterDTO.getId())){
                v.setDay(counterDTO.getDay());
                v.setWeek(v.getWeekOld() + counterDTO.getDay());
                v.setMonth(v.getMonthOld() + counterDTO.getDay());
                v.setYear(v.getYearOld() + counterDTO.getDay());
            }
        });
        redisCache.setCacheObject("Counter:" + username, counterList);


    }

    /**
     * 计数器计划
     * */
    @Override
    public void planCounter(EvsiCounterDTO counterDTO) {
        if (counterDTO.getPlan() < 0) {
            throw new CustomException("计划数量不可小于0");
        }
        // TODO 查询用户名
        String username = "1085155177@qq.com";
        List<EvsiCounterDTO> counterList = redisCache.getCacheObject("Counter:" + username);

        counterList.forEach(v -> {
            if (ObjectUtil.equal(v.getId(), counterDTO.getId())) {
                v.setPlan(counterDTO.getPlan());
            }
        });
        redisCache.setCacheObject("Counter:" + username, counterList);
    }

    /**
     * 计数器删除
     * */
    @Override
    public void delCounter(String[] ids) {
        String username = "1085155177@qq.com";
        List<EvsiCounterDTO> counterList = redisCache.getCacheObject("Counter:" + username);
        for (int i=0; i<ids.length; i++) {
            for (int k=0; k<counterList.size(); k++){
                if (ObjectUtil.equal(ids[i], counterList.get(k).getId())) {
                    counterList.remove(k);
                }
            }
        }
        System.out.println(counterList);
        redisCache.setCacheObject("Counter:" + username, counterList);
    }


}
